﻿using MESPro.Global;
using MESPro.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MESPro.DBManager
{
    public class FunctionManager
    {
        #region 曾
        /// <summary>
        /// 添加一个功能页面,不包括子类(add=httppost)
        /// </summary>
        /// <returns>返回插入成功的行数</returns>
        public int AddFunction(Function function)
        {
            var res = DbContext.Db.Insertable(function).ExecuteCommand();
            return res;
        }
        /// <summary>
        /// 添加一个功能,包括子类(add=httppost)
        /// </summary>
        /// <returns>返回插入成功的行数</returns>
        public bool AddFunctionInfo(Function FunctionGroup)
        {
            var res = DbContext.Db.InsertNav(FunctionGroup)
                .Include(x => x.DbControlGs, new InsertNavOptions() { OneToManyIfExistsNoInsert = true })
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除一个页面(导航删除)和他的子页面(Delete=HttpDelete)
        /// </summary>
        /// <returns>返回是否删除成功</returns>
        public bool DeleteFunction(Function function)
        {
            List<Function> list = new List<Function> { function };
            var res = DbContext.Db
                .DeleteNav<Function>(a => a.Id == function.Id)
                .Include(a => a.DbControlGs)
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 改
        /// <summary>
        /// 修改一个页面和他的子页面(//Update==httpput)
        /// </summary>
        /// <returns></returns>
        public bool UpdateFunctionInfo(Function function)
        {
            List<Function> list = new List<Function>() { function };
            var res = DbContext.Db
                .UpdateNav(list)
                .Include(z1 => z1.DbControlGs)
                .ExecuteCommand();
            return res;
        }
        #endregion

        #region 查
        /// <summary>
        /// 查询所有的页面,以及子类
        /// </summary>
        /// <returns></returns>
        public List<Function> GetFunctionListInfo()
        {
            var res = DbContext.Db
                .Queryable<Function>()
                .Includes(a => a.DbControlGs)
                .ToList();
            return res;
        }
        /// <summary>
        /// 查询所有的页面,不包括子类
        /// </summary>
        /// <returns></returns>
        public List<Function> GetFunctionList()
        {
            var res = DbContext.Db.Queryable<Function>().ToList();
            return res;
        }
        #endregion
    }
}
